package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.Statement;

public class JDBCTest {

	public static void main(String[] args) {
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return;
		} 

		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		
		try {
			
			conn = DriverManager.getConnection("url","sa","1234");
			conn.setAutoCommit(false);
			stmt = conn.createStatement(
					ResultSet.TYPE_SCROLL_SENSITIVE, 
					ResultSet.CONCUR_UPDATABLE,
					ResultSet.HOLD_CURSORS_OVER_COMMIT
			);
			
			stmt.executeQuery("SQL");
			
			stmt.addBatch("sql1");
			stmt.addBatch("sql2");
			stmt.executeBatch();
			
			rs = stmt.executeQuery("querySql");
			rs.setFetchDirection(ResultSet.FETCH_REVERSE);
			
			conn.commit();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				if(rs!=null) rs.close();
				if(stmt!=null) stmt.close();
				if(conn!=null) { conn.close(); conn.rollback(); }
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
	}
}


class JDBCTest2 {

	public static void main(String[] args) {
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return;
		} 

		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		
		try {
			conn = DriverManager.getConnection("url","sa","1234");
			pstmt = conn.prepareStatement(
					"oneSql", 
					ResultSet.TYPE_SCROLL_INSENSITIVE, 
					ResultSet.CONCUR_READ_ONLY, 
					ResultSet.CLOSE_CURSORS_AT_COMMIT);
			
			rs = pstmt.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				if(rs!=null) rs.close();
				if(pstmt!=null) pstmt.close();
				if(conn!=null) { conn.close(); conn.rollback(); }
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
	}

}

